package homework.score;

import java.util.EnumMap;
import java.util.Map;

public class EnumScore implements IScore<Course> {

    private Map<Course, Double> scores = new EnumMap<Course, Double>(Course.class);

    @Override
    public void setScore(Course name, double v) {
        if (name.validate(v)) {
            scores.put(name, v);
        } else {
            throw new IllegalArgumentException("invalid homework.score: " + v);
        }
    }

    @Override
    public double getScore(Course name) {
        Double score = scores.get(name);

        return score == null ? -1 : score;
    }

    @Override
    public double removeScore(Course name) {
        return scores.remove(name);
    }

    @Override
    public double sum() {
//        double s = 0;
//
//        for (double v : scores.values()) {
//            s += v;
//        }
//
//        return s;

        return scores.values().stream().reduce((a, b) -> a + b).get();
    }

    @Override
    public double average() {
        return sum() / scores.size();
    }

}
